package com.ants.boot.wx.service.impl;

import com.ants.boot.utils.TreeNodeUtils;
import com.ants.boot.wx.dao.WxAreaCodeMapper;
import com.ants.boot.wx.domain.WxAreaCode;
import com.ants.boot.wx.service.WxAreaCodeService;
import com.ants.boot.wx.vo.wxareacode.ListAreaCodeVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 蚂蚁会花呗
 * @date 2022/6/2 10:18
 */
@Service
public class WxAreaCodeServiceImpl extends ServiceImpl<WxAreaCodeMapper, WxAreaCode> implements WxAreaCodeService {
    @Override
    public ListAreaCodeVo listAreaCode() {
        ListAreaCodeVo vo  = new ListAreaCodeVo();
        List<WxAreaCode> list = this.list();
        List<WxAreaCode> provinces = list.stream().filter(province -> "1".equals(province.getType())).collect(Collectors.toList());
        List<WxAreaCode> citys = list.stream().filter(city -> "2".equals(city.getType())).collect(Collectors.toList());
        List<WxAreaCode> countys = list.stream().filter(county -> "3".equals(county.getType())).collect(Collectors.toList());
        vo.setProvinces(provinces);
        vo.setCitys(citys);
        vo.setCountys(countys);
        return vo;
    }

    @Override
    public List<WxAreaCode> treeAreaCode() {
        List<WxAreaCode> list = this.list();
        List<?> tree = new TreeNodeUtils(list).buildTree("code","pcode","0","children");
        return (List<WxAreaCode>) tree;
    }
}
